package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.person.Constants;
import com.person.common.InitApplication;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysProperty;
import com.person.ntv.entity.sys.SysUser;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysPropertyService;

@Controller
@RequestMapping("/system/sysProperty")
public class SysPropertyController {
	@Autowired
  private SysPropertyService sysPropertyService;
	@Autowired
  private CommonService commonService;
  
  @RequestMapping("/listPage")
	public String  getPropertyListPage(Map<String,String> params,ModelMap mv){
	    initCode(mv);
		return "/system/property/propertyList";
	}
	
	@RequestMapping("/propertyList")
	@ResponseBody
	public Map<String,Object> getPropertyList(PageParams myPage){
		Map<String,Object> map=new HashMap<String,Object>();
		Page<SysProperty> page=sysPropertyService.getSysProperties(myPage);
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
		return map;
	}
	
	@RequestMapping("/addPage")
	public String  addPropertyPage(String pId,ModelMap mv){
		initCode(mv);
		return "/system/property/propertyAdd";
	}
	@RequestMapping("/editPage")
	public String  editPropertyPage(String id,ModelMap mv){
		try {
	    initCode(mv);
	    SysProperty property = sysPropertyService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("property",property);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/property/propertyEdit";
	}
	
	@RequestMapping("/addProperty")
	public String  addProperty(ModelMap mv,SysProperty sysProperty){
		try {
			sysPropertyService.insertSelective(sysProperty);
			if("1".equals(sysProperty.getPropertyType()))
			InitApplication.sysProperties=sysPropertyService.getSysAllProperties();
			else{
				
			}
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/property/propertyAdd";
	}
	
	@RequestMapping("/editProperty")
	public String  editProperty(ModelMap mv,SysProperty sysProperty){
		try {
			if("1".equals(sysProperty.getPropertyType()))
			sysPropertyService.updateByPrimaryKeySelective(sysProperty);
            else{
				
			}
			InitApplication.sysProperties=sysPropertyService.getSysAllProperties();
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/property/propertyEdit";
	}
	
	@RequestMapping("/delProperty")
	@ResponseBody
	public Map<String,Object>  delProperty(BigDecimal[]ids,HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			sysPropertyService.deleteByPrimaryKeys(ids); 
			map.put("error", "0");
			map.put("msg","删除成功！");
		} catch (ServiceException e) {
			map.put("error", "1");
			map.put("msg",e.getMessage()+"删除失败！");
		}
		return map;
	}
	
	@RequestMapping("/detailPage")
	public String  detailPropertyPage(String id,ModelMap mv){
		try {
	    SysProperty property = sysPropertyService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("property",property);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/property/propertyDetail";
	}
	
	@RequestMapping("/validatePropertyName")
	@ResponseBody
	public boolean validatePropertyName(SysProperty  sysProperty) throws Exception{
		try {
			if(sysProperty.getPropertyName()==null)sysProperty.setPropertyName("");
			int cnt=commonService.validateOne("sys_property","property_name", sysProperty.getPropertyName(),sysProperty.getId()==null?null:String.valueOf(sysProperty.getId()));
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
           e.printStackTrace();
		}
		return false;
	}
	
	//保存个人属性
	@RequestMapping("/addUserProperty")
	@ResponseBody
	public Map<String,Object>addUserProperty(HttpServletRequest request,SysProperty sysProperty){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
	       SysUser user=(SysUser)request.getSession().getAttribute(Constants.CURRENT_USER);
	       map.put("propertyId", sysProperty.getId());
	       map.put("userId", user.getId());
	       map.put("propertyValue", sysProperty.getPropertyValue());
		   sysPropertyService.addUserProperty(map);
		   
		   map.put("error", "0");
		   map.put("msg","保存成功！");
		} catch (ServiceException e) {
			map.put("error", "1");
			map.put("msg",e.getMessage()+"保存失败！");
		}
		return map;
	}
	
	private void initCode(ModelMap mv){
		mv.addAttribute("SysPropertyType",JSONArray.fromObject(commonService.getCodeList("SysPropertyType",true)).toString()); 
	}
}
